var express = require('express');
var router = express.Router();
var sqlModel = require('../lib/sql');

// 查询学生列表
router.post('/studentList', async(req, res, next) => {
    await sqlModel.findStuList(req.body)
        .then(result => {
            // user = result[0];
            let obj = {};
            obj.totalCount = result[0][0].NUMS;
            obj.result = result[1];
            var response = { status: 200, data: obj };
            res.send(response);
        }).catch((err)=>{
            res.send(err);
        })   
})

// 新增学生
router.post('/createStudent', async(req, res, next) => {
    let msg = '';
    const { name, sex, phone, type, birthday, notes, studentNo, portrait } = req.body;
    if(!name) {
        msg = '姓名不能为空'
    }
    if(!phone) {
        msg = '手机号不能为空'
    }
    if(msg) {
        let result = {
            code: 400,
            msg: msg
        }
        res.send(result);
        return
    }
    await sqlModel.createStudent([name, sex, phone, type, birthday, notes, studentNo, JSON.stringify(portrait)])
        .then(result => {
            var response = { status: 200, data: result };
            res.send(response);
        }).catch((err)=>{
            res.send(err);
        })
})

// 查询菜单
router.post('/findMenuList', async(req, res, next) => {
    await sqlModel.findMenuList()
        .then(result => {
            var response = { status: 200, data: result };
            res.send(response);
        }).catch((err)=>{
            res.send(err);
        })
})

module.exports = router;
